#include <iostream>
#include <queue>

using namespace std;

typedef unsigned long long int ulli;

typedef priority_queue<ulli, vector<ulli>, greater<ulli> >   minheap;

// Funcion Principal
int main (){
  
	int cantidad;
	ulli temp;
	ulli s1;
	ulli s2;
	ulli res;
	ulli value;
	minheap heap;

	cin>>cantidad;
  
	while (cantidad != 0){ 
      
		for(int i = 0; i < cantidad; i++){
			cin >> value;
			heap.push(value); 
		}	  
		if (cantidad == 1){
			cout << 0 << endl;
		} else{
			res = 0;
			temp = 0;
			while (heap.size() > 1){
				s1 = heap.top();
				heap.pop();
                s2 = heap.top();
				heap.pop();
                temp = s1 + s2;
                heap.push(temp);
                res += temp;
			}
            heap.pop();
            cout << res << endl;
		}
		cin >> cantidad;  
	}
  
	return 0;
}
